const app = getApp();
const api = app.globalData.api


Page({
    data: {
        purchaseAmount: {
            lazyLoad: true
        },
        deliveryRate: {
            lazyLoad: true
        },
        qualityRate: {
            lazyLoad: true
        },
        purchaseDateRange: '',
        deliveryRateRange: '',
        qualityRateRange: '',
        supplier: {},
        material: {}
    },
    onLoad: async function () {
        await this.getStatData()
        await this.getTopData()
        await this.getDeliveryRate()
        await this.getQualityRate()
        await this.dataBoard()
    },
    onChange: async function (obj) {
        let type = obj.detail.name
        if (type) {
            switch (type) {
                case 'supplier':
                    this.getTopData(type)
                    break
                case 'material':
                    this.getTopData(type)
                    break
            }
        }
    },
    async dataBoard() {
        const res = await api.curlGet('?cla=purchaseStatistics&fun=dataBoard')
        if (res.code === 1) {
            this.setData({
                result: res.data
            })
        } else {
            api.showToast(res.warn)
        }
    },
    // 获取统计数据
    getStatData: async function (params = {}) {
        let that = this
        const res = await api.curlPost('?cla=purchaseStatistics', params)
        if (res.code === 1) {
            that.setData({
                purchaseDateRange: res.data.dateRange
            })
            return api.wxCharts(that.selectComponent('#purchaseAmount'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    // 按期交货率
    getDeliveryRate: async function (params = {}) {
        let that = this
        const res = await api.curlPost('?cla=purchaseStatistics&fun=deliveryRate', params)
        if (res.code === 1) {
            that.setData({
                deliveryRateRange: res.data.dateRange
            })
            return api.wxCharts(that.selectComponent('#deliveryRate'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    // 来料质检合格率
    getQualityRate: async function (params = {}) {
        let that = this
        const res = await api.curlPost('?cla=purchaseStatistics&fun=qualityRate', params)
        if (res.code === 1) {
            that.setData({
                qualityRateRange: res.data.dateRange
            })
            return api.wxCharts(that.selectComponent('#qualityRate'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    getTopData: async function (type = 'supplier', params = {}) {
        let that = this
        const res = await api.curlPost('?cla=purchaseStatistics&fun=' + type, params)
        if (res.code === 1) {
            const another = (type === 'material') ? 'supplier' : 'material'
            that.setData({
                [another]: {},
                [type]: res.data
            })
        }
    },
    // 搜索
    onSearch: async function (e) {
        if (e.detail) {
            let that = this
            const res = await api.curlPost('?cla=purchaseStatistics&fun=orderToId', {orderNo: e.detail})
            if (res.code !== 1) {
                api.showToast(res.warn, 'none')
            } else {
                wx.navigateTo({
                    url: 'detail?id=' + res.data,
                })
            }
        }
    },
    // 扫码
    onScan: async function (e) {
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {
                if (res.errMsg === "scanCode:ok") {
                    const string = res.result
                    let array = api.parseStr(string)
                    if (!['purchase_order', 'purchaseOrder'].includes(array['source'])) {
                        api.showToast('暂不支持扫描非采购订单二维码', 'none')
                    } else {
                        wx.navigateTo({
                            url: '/pages/purchase/detail?id=' + array['id'],
                        })
                    }
                }
            }
        })
    },
})